package com.jushu.storbox.task.tqueue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.jushu.storbox.callback.IDownloadThumbnailCallback;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.model.request.ImageThumRequest;
import com.huawei.sharedrive.sdk.android.service.FileClient.ThumbDownloadProcessor;
import com.huawei.sharedrive.sdk.android.serviceV2.FileClientV2;

public class DownloadThumbnailTask implements Runnable {

	private final static String LOG_TAG = "DownloadThumbnailTask";
//	private final Context context;
	private final String authorization;
	private final String ownerID;
	private final String fileId;
	private final String iconPath;
	private IDownloadThumbnailCallback callback = null;
	private boolean isSuccess = false;
	private FileClientV2 client = null;
	
	public DownloadThumbnailTask(String authorization, String ownerID, String fileId, String iconPath, IDownloadThumbnailCallback callback) {
		// TODO Auto-generated constructor stub
//		this.context = context;
		this.authorization = authorization;
		this.ownerID = ownerID;
		this.fileId = fileId;
		this.iconPath = iconPath;
		if (null == callback) {
			callback = new IDownloadThumbnailCallback() {
				
				@Override
				public void onSuccess(String fileId) {
					// TODO Auto-generated method stub
				}
				
				@Override
				public void onStart() {
					// TODO Auto-generated method stub
				}
				
				@Override
				public void onProgress(int currentProgress, long currentSize, long maxSize) {
					// TODO Auto-generated method stub
				}
				
				@Override
				public void onFailure(Throwable t) {
					// TODO Auto-generated method stub
				}
			};
		}
		this.callback = callback;
		if (null == client)
			client = FileClientV2.getInstance();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
//		File rootFolder = new File(DirectoryTools.getCurrentThumbnailPath(context));
//		if (!rootFolder.exists())
//			rootFolder.mkdirs();
		ImageThumRequest request = new ImageThumRequest();
		request.setFileId(fileId);
		request.setOwnerID(ownerID);
		request.setMinHeight(ClientConfig.THUMBNAIL_HEIGHT);
		request.setMinWidth(ClientConfig.THUMBNAIL_WIDTH);
		ThumbDownloadProcessor processor = new ThumbDownloadProcessor() {

			@Override
			public void onSuccess(InputStream is) {
				// TODO Auto-generated method stub
				isSuccess = false;
				callback.onStart();// start
				FileOutputStream fos = null;
				File file = new File(iconPath);
				try {
					//
					if (file.exists())
						file.delete();
					File folder = new File(file.getParent());
					if (!folder.exists())
						folder.mkdirs();
					if (!file.exists())
						file.createNewFile();
					fos = new FileOutputStream(file);
					int len = 0;
					long currentProgress = 0;
					byte[] buff = new byte[1024 * 12];
					callback.onProgress((int)0, currentProgress, currentProgress);//0%
					while ((len = is.read(buff)) != -1) {
						fos.write(buff, 0, len);
						currentProgress += len;
					}
					callback.onProgress((int)100, currentProgress, currentProgress);//0%
					isSuccess = true;
				} catch (Exception e) {
					e.printStackTrace();
					isSuccess = false;
					LogUtil.e(LOG_TAG,
							"[" + iconPath + "]" + e.getLocalizedMessage());
					callback.onFailure(e.fillInStackTrace());// onFail
				} finally {
					try {
						fos.close();
						if (isSuccess) {
							LogUtil.i(LOG_TAG, "[" + iconPath + "]onSuccess:");
							callback.onSuccess(fileId);// onSuccess
						} else {
							LogUtil.e(LOG_TAG, "[" + iconPath + "]onSuccess fail, need to delete it:"
									+ file.length());
							if (file.exists())
								file.delete();
						}
					} catch (Exception e) {
						e.printStackTrace();
						isSuccess = false;
						LogUtil.e(LOG_TAG,
								"fos.close()[" + iconPath + "]" + e.getLocalizedMessage());
						callback.onFailure(e.fillInStackTrace());// onFail
					}
				}
			}

			@Override
			public void onFailure(Exception e) {
				// TODO Auto-generated method stub
				e.printStackTrace();
				isSuccess = false;
				LogUtil.e(LOG_TAG, "onFailure[" + iconPath + "]" + e.getLocalizedMessage());
				File file = new File(iconPath);
				if (file.exists())
					file.delete();
				callback.onFailure(e.fillInStackTrace());// onFail
			}
		};
		client.downloadImageThum(request, authorization, processor);
	}
	
	// callback
	public void setCallback(IDownloadThumbnailCallback callback) {
		this.callback = callback;
	}
}
